package com.hh.core.shiro.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.hh.admin.entity.SysRole;
import com.hh.admin.entity.SysUser;
import com.hh.admin.enums.SysUserStatus;
import com.hh.admin.exception.LoginErrorException;
import com.hh.admin.mapper.SysMenuMapper;
import com.hh.admin.mapper.SysRoleMapper;
import com.hh.admin.mapper.SysUserMapper;
import com.hh.core.enums.ExceptionEnum;
import com.hh.core.shiro.entity.ShiroUser;
import com.hh.core.shiro.service.IShiroService;

@Service
@Transactional(readOnly = true)
public class ShiroServiceImpl implements IShiroService {

	@Autowired
	SysUserMapper sysUserMapper;

	@Autowired
	SysMenuMapper sysMenuMapper;

	@Autowired
	SysRoleMapper sysRoleMapper;

	@Override
	public SysUser getSysUser(String account) {
		SysUser user = sysUserMapper.getByAccount(account);
		if (ObjectUtil.isNull(user)) {
			throw new LoginErrorException(ExceptionEnum.USER_NOT_EXIST); // 账号不存在
		}

		if (user.getStatus().equals(SysUserStatus.CANCEL.getCode())) {
			throw new LoginErrorException(ExceptionEnum.USER_FREEZED); // 账号被注销
		}

		if (user.getStatus().equals(SysUserStatus.FREEZED.getCode())) {
			throw new LoginErrorException(ExceptionEnum.USER_FREEZED); // 账号被冻结
		}

		return user;
	}

	@Override
	public ShiroUser getShiroUser(SysUser user) {
		ShiroUser shiroUser = new ShiroUser();

		shiroUser.setId(user.getId());
		shiroUser.setAccount(user.getAccount());// 账号
		shiroUser.setName(user.getUserName()); // 用户名称
		shiroUser.setImagePath(user.getImage()); //头像地址
		shiroUser.setGroupId(user.getSysGroup().getId()); // 组织机构ID
		shiroUser.setGroupName(user.getSysGroup().getGroupName()); // 组织机构名称
		Set<Long> roleIdList = new HashSet<Long>();
		Set<String> roleCodeList = new HashSet<String>();
		Set<String> menuList = new HashSet<String>();
		for (SysRole role : user.getRoleList()) {
			roleIdList.add(role.getId());
			roleCodeList.add(role.getRoleCode());
			List<String> urlList =sysMenuMapper.getUrlByRoleId(role.getId());
			//获取该角色下权限url
        	if(ObjectUtil.isNotNull(urlList)){
        		for (String url : urlList) {
        			if(StrUtil.isNotBlank(url)){
        				menuList.add(url);
        			}
        		}
        	}
        	shiroUser.setRoleName(role.getRoleName());
		}
		shiroUser.setRoleList(roleIdList); // 角色ID
		shiroUser.setRoleCode(roleCodeList); // 角色名称
		shiroUser.setMenuList(menuList);   //该角色下权限url集
		return shiroUser;
	}

	@Override
	public List<String> findPermissionsByRoleId(Long roleId) {
		return sysMenuMapper.getUrlByRoleId(roleId);
	}

	@Override
	public String findRoleNameByRoleId(Long roleId) {
		SysRole role = sysRoleMapper.selectById(roleId);
		return role.getRoleName();
	}

	@Override
	public SimpleAuthenticationInfo getAuthenticationInfo(ShiroUser shiroUser,
			SysUser user, String realmName) {
		String credentials = user.getPassword();
		// 密码加盐处理
		String source = user.getSalt();
		ByteSource credentialsSalt = new Md5Hash(source);
		return new SimpleAuthenticationInfo(shiroUser, credentials,
				credentialsSalt, realmName);
	}

}
